//
//  CYTabBarViewController.m
//  CYTabBarViewController
//
//  Created by lc4y on 2017/12/2.
//  Copyright © 2017年 weiwang. All rights reserved.
//

#import "CYTabBarViewController.h"
#import "CYTopTitleBaseLineViewController.h"
#import "CYTopTitleBGColorViewController.h"
#import "CYBottomViewController.h"
#import <objc/runtime.h>

static char *TabBarItemKey = "TabBarItemKey";
static char *TabBarControllerKey = "TabBarControllerKey";

@implementation UIViewController (CYTabBarItem)

-(void)setCy_tabBarItem:(CYTabBarItem *)cy_tabBarItem {
    objc_setAssociatedObject(self, TabBarItemKey, cy_tabBarItem, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (CYTabBarItem *)cy_tabBarItem {
    if( !objc_getAssociatedObject(self, TabBarItemKey) ){
        [self setCy_tabBarItem:[[CYTabBarItem alloc] init]];
    }
    
    return objc_getAssociatedObject(self, TabBarItemKey);
}

-(void)setCy_tabBarController:(CYTabBarViewController *)cy_tabBarController {
    objc_setAssociatedObject(self, TabBarControllerKey, cy_tabBarController, OBJC_ASSOCIATION_ASSIGN);
}

- (CYTabBarViewController *)cy_tabBarController {
    return objc_getAssociatedObject(self, TabBarControllerKey);
}

@end

@interface CYTabBarViewController () <CYBaseCollectionViewControllerDelegate>

@property (nonatomic,strong) CYBaseCollectionViewController *topViewController;
@property (nonatomic,strong) CYBottomViewController *bottomViewController;
@property (nonatomic, strong, readwrite) CYTitleView *titleView;

@end

@implementation CYTabBarViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.titleView = [CYTitleView titleView];
        self.titleView.cy_TopStyle = CYTopTitleViewStyleBaseLine;
    }
    return self;
}

- (instancetype)initWithTopStyle:(CYTopTitleViewStyle)style {
    self = [super init];
    if (self) {
        self.titleView = [CYTitleView titleView];
        self.titleView.cy_TopStyle = style;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0];
}

- (void)setCy_viewControllers:(NSArray *)cy_viewControllers {
    _cy_viewControllers = [cy_viewControllers copy];
    [self cy_configSubViewControllers];
}

- (void)cy_configSubViewControllers {
    if(self.titleView.cy_TopStyle == CYTopTitleViewStyleBaseLine) {
        self.topViewController = (CYTopTitleBaseLineViewController *)[self cy_addViewController:@"CYTopTitleBaseLineViewController"];
    } else if (self.titleView.cy_TopStyle == CYTopTitleViewStyleBGColor) {
        self.topViewController = (CYTopTitleBGColorViewController *)[self cy_addViewController:@"CYTopTitleBGColorViewController"];
    }
    
    self.bottomViewController = (CYBottomViewController *)[self cy_addViewController:@"CYBottomViewController"];
    [self.topViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        if(isiPhoneX) {
            make.top.mas_equalTo(84);
        } else {
            make.top.mas_equalTo(64);
        }
        
        make.height.mas_equalTo(50);
    }];
    
    [self.bottomViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.topViewController.view.mas_bottom);
        make.bottom.mas_equalTo(0);
    }];
    
    for (UIViewController *viewC in self.cy_viewControllers) {
        viewC.cy_tabBarController = self;
    }
    
    self.topViewController.delegate = self;
    self.bottomViewController.delegate = self;
    
    [self.topViewController setContentArray:self.cy_viewControllers];
    [self.bottomViewController setContentArray:self.cy_viewControllers];
    
    self.topViewController.cy_currentIndex = self.cy_selectedIndex;
    self.bottomViewController.cy_currentIndex = self.cy_selectedIndex;
}

- (UIViewController *)cy_addViewController:(NSString *)className{
    UIViewController *viewController = [NSClassFromString(className) new];
    viewController.cy_tabBarController = self;
    [self.view addSubview:viewController.view];
    [self addChildViewController:viewController];
    [viewController didMoveToParentViewController:self];
    
    return viewController;
}


- (void)setCy_selectedIndex:(NSInteger)cy_selectedIndex {
    if(_cy_selectedIndex == cy_selectedIndex ){
        return;
    }
    _cy_selectedIndex = cy_selectedIndex;
    self.topViewController.cy_currentIndex = self.cy_selectedIndex;
    self.bottomViewController.cy_currentIndex = self.cy_selectedIndex;
}

- (void)cy_showInViewController:(UIViewController *)viewController {
    [viewController.view addSubview:self.view];
    [viewController addChildViewController:self];
    [viewController.view sendSubviewToBack:self.view];
    [self didMoveToParentViewController:viewController];
}

- (void)topTitleViewMenuClick:(NSInteger)buttonIndex {
    self.cy_selectedIndex = buttonIndex;
}

- (void)bottomViewMenuClick:(NSInteger)buttonIndex {
    self.cy_selectedIndex = buttonIndex;
}

- (void)cy_freeSubViewController {
    
    
    self.topViewController.cy_tabBarController = nil;
    self.topViewController = nil;
    self.bottomViewController.cy_tabBarController = nil;
    self.bottomViewController = nil;
    for (UIViewController *viewC in self.cy_viewControllers) {
        viewC.cy_tabBarController = nil;
        [viewC removeFromParentViewController];
        [viewC.view removeFromSuperview];
    }
    
    [self removeFromParentViewController];
    [self.view removeFromSuperview];
    self.cy_viewControllers = nil;
}

- (void)dealloc {
    NSLog(@"%@ dealloc", NSStringFromClass([self class]));
}

@end

@implementation CYTabBarItem

@end
